using System;
using Gtk;
using System.Threading;
using RaspberryPiDotNet;

public partial class MainWindow: Gtk.Window
{	
	int countP1 = 0;
	int countP2 = 0;
	GPIOMem GPIO9 = new GPIOMem(GPIOPins.Pin_P1_21);
	GPIOMem GPIO10 = new GPIOMem(GPIOPins.Pin_P1_19);

	public MainWindow (): base (Gtk.WindowType.Toplevel)
	{
		Build ();

		/* Mike Parks wuz here yo. */
		Thread thr = new Thread (new ThreadStart (ThreadRoutine));
		thr.Start ();
	}

	protected void ThreadRoutine ()
	{
		while (true)
		{
			if (GPIO9.Read() && GPIO10.Read())
			{
				countP1 = 0;
				countP2 = 0;
				Thread.Sleep(300);
			}
			if (GPIO9.Read())
			{
				countP1 += 1;
				Thread.Sleep(300);
			}
			else if (GPIO10.Read())
			{
				countP2 += 1;
				Thread.Sleep(300);
			}

			Gtk.Application.Invoke (delegate {

				lblScoreP1.LabelProp = @"<span font_size=""250000"">" + countP1 + "</span>";
				lblScoreP2.LabelProp = @"<span font_size=""250000"">" + countP2 + "</span>";
			});
		}
	}

	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}
}
